#include "Rp2Renderer.h"

using namespace Rp2;

GLenum Renderer::ms_aeStencilCompare[StencilState::CF_QUANTITY] =
{
    GL_NEVER,     // StencilState::CF_NEVER
    GL_LESS,      // StencilState::CF_LESS
    GL_EQUAL,     // StencilState::CF_EQUAL
    GL_LEQUAL,    // StencilState::CF_LEQUAL
    GL_GREATER,   // StencilState::CF_GREATER
    GL_NOTEQUAL,  // StencilState::CF_NOTEQUAL
    GL_GEQUAL,    // StencilState::CF_GEQUAL
    GL_ALWAYS     // StencilState::CF_ALWAYS
};

GLenum Renderer::ms_aeStencilOperation[StencilState::OT_QUANTITY] =
{
    GL_KEEP,      // StencilState::OT_KEEP
    GL_ZERO,      // StencilState::OT_ZERO
    GL_REPLACE,   // StencilState::OT_REPLACE
    GL_INCR,      // StencilState::OT_INCREMENT
    GL_DECR,      // StencilState::OT_DECREMENT
    GL_INVERT     // StencilState::OT_INVERT
};

//---------------------------------------------------------------------------------------------------
void Renderer::SetStencilState(StencilState* pkStencilState)
{
    m_aspkState[GlobalState::STENCIL] = pkStencilState;

	if (pkStencilState->Enabled)
	{
		glEnable(GL_STENCIL_TEST);
		
		glStencilFunc(ms_aeStencilCompare[pkStencilState->Compare],
			(int)pkStencilState->Reference, pkStencilState->Mask);
		
		glStencilMask(pkStencilState->WriteMask);

		glStencilOp(ms_aeStencilOperation[pkStencilState->OnFail],
			ms_aeStencilOperation[pkStencilState->OnZFail],
			ms_aeStencilOperation[pkStencilState->OnZPass]);
	}
	else
	{
		glDisable(GL_STENCIL_TEST);
	}
}
//---------------------------------------------------------------------------------------------------